$(function(){
        $('#dg').datagrid({    
	    url:'../../course_queryPaginator.action', 
	    singleSelect: true,  
	    pageSize:5,
	    pageList:[5,10,20,30,40,50],
	    columns:[[  
	        {field:'id ',title:'id',width:100,checkbox:true},     
	        {field:'name',title:'课程名',width:100,align:'center'},    
	        {field:'credit',title:'学分',width:100,align:'center'}    
	    ]], 
	        toolbar: [{
	        iconCls: 'icon-add',
	        text:'新增课程',
	        handler: function() {
            	if($('#addCourse')[0]==null){
            		$('<div id="addCourse"></div>').appendTo('body');
            	}
	        	$("#addCourse").dialog({
         		   href:'addCourse.jsp',
             	   title: '新增课程',
 	       		    width: 300,
 	       		    height: 200,
 	       		    cache:false,
 	       		    modal:true,
 	       		    buttons:[{
 	       				text:'保存',
 	       				iconCls:'icon-ok',
	    	       				handler:function(){
	    			 				if(!$('#addCourse2').form('validate')){
	    			 					return ;
	    			 				}
	    	       					var addCourse = {
	    	       						name:$('#name').val(),
	    	       						credit:$('#credit').val()
	    	       					};
	    	       					$.post("../../course_add.action",addCourse,function(data){
	    	       						if(data.returnmsg=="success"){
	    	       							$('#addCourse').dialog('destroy');
	    	       							$("#dg").datagrid('load');
	    	       							alert("课程添加成功");
	    	       						}else{
	    	       								alert("新增课程失败！");
	    	       						}
	    	       					});
	    	       				}
 	       				},{
 	       				text:'取消',
 	       				iconCls:'icon-cancel',
 	       				handler:function(){
 	       					$('#addCourse').dialog('destroy');
 	       				}
 	       			}]
         	});
	        }
	    },{
	        iconCls: 'icon-remove',
	        text:'删除课程',
	        handler: function() {
	        	var row =$("#dg").datagrid('getSelected');
	        	if(row){
    	        	$.messager.confirm('确认','您确认想要删除课程'+row.name,function(flag){    
    	        	    if (flag){    
    	        	    	$.post("../../course_delete.action ",{courseId:row.id},function(data){
    		        			if(data.returnmsg=="success"){
    		        				$("#dg").datagrid('load');
    		        				alert("课程删除成功");
    		        			}else{
    		        					alert("删除课程失败！");
    		        			}
    		        		}); 
    	        	    }    
    	        	});  
	        	}else{
	                $.messager.show({
	                    title:'消息提示',
	                    msg:'数据未选中。',
	                    timeout:5000,
	                    showType:'slide'
	                });
	        	}  
	        }
	          
	    },{
	        iconCls: 'icon-edit',
	        text:'更改课程',
	        handler: function() {
            	if($('#updateCourse')[0]==null){
            		$('<div id="updateCourse"></div>').appendTo('body');
            	}
            	 var row = $('#dg').datagrid('getSelected');
  	            	if(row){
			        	$("#updateCourse").dialog({
		         		   href:'updateCourse.jsp',
		             	   title: '更改课程',
		 	       		    width: 300,
		 	       		    height: 200,
		 	       		    cache:false,
		 	       		    modal:true,
		 	       		    buttons:[{
		 	       				text:'保存',
		 	       				iconCls:'icon-ok',
			    	       				handler:function(){
			    	       					
			    			 				if(!$('#updateCourse2').form('validate')){
			    			 					return ;
			    			 				}
/*			    	       					var updateCourse = {
			    	       							id:$('#courseId').val(),
				    	       						name:$('#name2').val(),
				    	       						credit:$('#credit2').val()
				    	       				};
			    	       					alert(updateCourse.id);
			    	       					alert(updateCourse.name);
			    	       					alert(updateCourse.credit);*/
			    			 				var updateCourse =$("#updateCourse2").serialize();
			    	       					$.post("../../course_update.action",updateCourse,function(data){
			    	       						if(data.returnmsg=="success"){
			    	       							$('#updateCourse').dialog('destroy');
			    	       							$("#dg").datagrid('load');
			    	       							alert("课程更改成功");
			    	       						}else{
			    	       								alert("更新课程失败！");
			    	       						}
			    	       					});
			    	       				}
		 	       				},{
		 	       				text:'取消',
		 	       				iconCls:'icon-cancel',
		 	       				handler:function(){
		 	       					$('#updateCourse').dialog('destroy');
		 	       				}
		 	       			}]
		         	});	
  	            }else{
	                $.messager.show({
	                    title:'消息提示',
	                    msg:'数据未选中。',
	                    timeout:5000,
	                    showType:'slide'
	                });
	
	            }
	        }
	    }],
	    fitColumns: true,
	    pagination: true,
	    fitColumns: true,
	    striped: true,
	    rownumbers: true   
	});  
})
